<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Iterative query</title>
<link rel="stylesheet" href="../../../../../../../doc/src/boostbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
<link rel="home" href="../../../index.html" title="Chapter 1. Geometry">
<link rel="up" href="../rtree_examples.html" title="Examples">
<link rel="prev" href="range_adaptors.html" title="Range adaptors">
<link rel="next" href="index_stored_in_shared_memory_using_boost_interprocess.html" title="Index stored in shared memory using Boost.Interprocess">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr>
<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../../boost.png"></td>
<td align="center"><a href="../../../../../../../index.html">Home</a></td>
<td align="center"><a href="../../../../../../../libs/libraries.htm">Libraries</a></td>
<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
<td align="center"><a href="../../../../../../../more/index.htm">More</a></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="range_adaptors.html"><img src="../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../rtree_examples.html"><img src="../../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../../index.html"><img src="../../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="index_stored_in_shared_memory_using_boost_interprocess.html"><img src="../../../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="geometry.spatial_indexes.rtree_examples.iterative_query"></a><a class="link" href="iterative_query.html" title="Iterative query">Iterative
        query</a>
</h4></div></div></div>
<p>
</p>
<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">geometry</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">geometry</span><span class="special">/</span><span class="identifier">geometries</span><span class="special">/</span><span class="identifier">point</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">geometry</span><span class="special">/</span><span class="identifier">geometries</span><span class="special">/</span><span class="identifier">box</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>

<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">geometry</span><span class="special">/</span><span class="identifier">index</span><span class="special">/</span><span class="identifier">rtree</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>

<span class="comment">// just for output</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">iostream</span><span class="special">&gt;</span>

<span class="keyword">namespace</span> <span class="identifier">bg</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">geometry</span><span class="special">;</span>
<span class="keyword">namespace</span> <span class="identifier">bgi</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">geometry</span><span class="special">::</span><span class="identifier">index</span><span class="special">;</span>

<span class="keyword">int</span> <span class="identifier">main</span><span class="special">()</span>
<span class="special">{</span>
    <span class="keyword">typedef</span> <span class="identifier">bg</span><span class="special">::</span><span class="identifier">model</span><span class="special">::</span><span class="identifier">point</span><span class="special">&lt;</span><span class="keyword">double</span><span class="special">,</span> <span class="number">2</span><span class="special">,</span> <span class="identifier">bg</span><span class="special">::</span><span class="identifier">cs</span><span class="special">::</span><span class="identifier">cartesian</span><span class="special">&gt;</span> <span class="identifier">point</span><span class="special">;</span>
    <span class="keyword">typedef</span> <span class="identifier">point</span> <span class="identifier">value</span><span class="special">;</span>
    <span class="keyword">typedef</span> <span class="identifier">bgi</span><span class="special">::</span><span class="identifier">rtree</span><span class="special">&lt;</span> <span class="identifier">value</span><span class="special">,</span> <span class="identifier">bgi</span><span class="special">::</span><span class="identifier">linear</span><span class="special">&lt;</span><span class="number">16</span><span class="special">&gt;</span> <span class="special">&gt;</span> <span class="identifier">rtree_t</span><span class="special">;</span>

    <span class="comment">// create the rtree using default constructor</span>
    <span class="identifier">rtree_t</span> <span class="identifier">rtree</span><span class="special">;</span>

    <span class="comment">// create some values</span>
    <span class="keyword">for</span> <span class="special">(</span> <span class="keyword">double</span> <span class="identifier">f</span> <span class="special">=</span> <span class="number">0</span> <span class="special">;</span> <span class="identifier">f</span> <span class="special">&lt;</span> <span class="number">10</span> <span class="special">;</span> <span class="identifier">f</span> <span class="special">+=</span> <span class="number">1</span> <span class="special">)</span>
    <span class="special">{</span>
        <span class="comment">// insert new value</span>
        <span class="identifier">rtree</span><span class="special">.</span><span class="identifier">insert</span><span class="special">(</span><span class="identifier">point</span><span class="special">(</span><span class="identifier">f</span><span class="special">,</span> <span class="identifier">f</span><span class="special">));</span>
    <span class="special">}</span>

    <span class="comment">// query point</span>
    <span class="identifier">point</span> <span class="identifier">pt</span><span class="special">(</span><span class="number">5.1</span><span class="special">,</span> <span class="number">5.1</span><span class="special">);</span>

    <span class="comment">// iterate over nearest Values</span>
    <span class="keyword">for</span> <span class="special">(</span> <span class="identifier">rtree_t</span><span class="special">::</span><span class="identifier">const_query_iterator</span>
            <span class="identifier">it</span> <span class="special">=</span> <span class="identifier">rtree</span><span class="special">.</span><span class="identifier">qbegin</span><span class="special">(</span><span class="identifier">bgi</span><span class="special">::</span><span class="identifier">nearest</span><span class="special">(</span><span class="identifier">pt</span><span class="special">,</span> <span class="number">100</span><span class="special">))</span> <span class="special">;</span>
            <span class="identifier">it</span> <span class="special">!=</span> <span class="identifier">rtree</span><span class="special">.</span><span class="identifier">qend</span><span class="special">()</span> <span class="special">;</span>
            <span class="special">++</span><span class="identifier">it</span> <span class="special">)</span>
    <span class="special">{</span>
        <span class="keyword">double</span> <span class="identifier">d</span> <span class="special">=</span> <span class="identifier">bg</span><span class="special">::</span><span class="identifier">distance</span><span class="special">(</span><span class="identifier">pt</span><span class="special">,</span> <span class="special">*</span><span class="identifier">it</span><span class="special">);</span>

        <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="identifier">bg</span><span class="special">::</span><span class="identifier">wkt</span><span class="special">(*</span><span class="identifier">it</span><span class="special">)</span> <span class="special">&lt;&lt;</span> <span class="string">", distance= "</span> <span class="special">&lt;&lt;</span> <span class="identifier">d</span> <span class="special">&lt;&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span>

        <span class="comment">// break if the distance is too big</span>
        <span class="keyword">if</span> <span class="special">(</span> <span class="identifier">d</span> <span class="special">&gt;</span> <span class="number">2</span> <span class="special">)</span>
        <span class="special">{</span>
            <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="string">"break!"</span> <span class="special">&lt;&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span>
            <span class="keyword">break</span><span class="special">;</span>
        <span class="special">}</span>
    <span class="special">}</span>

    <span class="keyword">return</span> <span class="number">0</span><span class="special">;</span>
<span class="special">}</span>
</pre>
<p>
        </p>
<h5>
<a name="geometry.spatial_indexes.rtree_examples.iterative_query.h0"></a>
          <span class="phrase"><a name="geometry.spatial_indexes.rtree_examples.iterative_query.expected_results"></a></span><a class="link" href="iterative_query.html#geometry.spatial_indexes.rtree_examples.iterative_query.expected_results">Expected
          results</a>
        </h5>
<pre class="programlisting"><span class="identifier">POINT</span><span class="special">(</span><span class="number">5</span> <span class="number">5</span><span class="special">),</span> <span class="identifier">distance</span><span class="special">=</span> <span class="number">0.141421</span>
<span class="identifier">POINT</span><span class="special">(</span><span class="number">6</span> <span class="number">6</span><span class="special">),</span> <span class="identifier">distance</span><span class="special">=</span> <span class="number">1.27279</span>
<span class="identifier">POINT</span><span class="special">(</span><span class="number">4</span> <span class="number">4</span><span class="special">),</span> <span class="identifier">distance</span><span class="special">=</span> <span class="number">1.55563</span>
<span class="identifier">POINT</span><span class="special">(</span><span class="number">7</span> <span class="number">7</span><span class="special">),</span> <span class="identifier">distance</span><span class="special">=</span> <span class="number">2.68701</span>
<span class="keyword">break</span><span class="special">!</span>
</pre>
</div>
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
<td align="left"></td>
<td align="right"><div class="copyright-footer">Copyright © 2009-2021 Barend Gehrels,
      Bruno Lalande, Mateusz Loskot, Adam Wulkiewicz, Oracle and/or its affiliates<p>
        Distributed under the Boost Software License, Version 1.0. (See accompanying
        file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>)
      </p>
</div></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="range_adaptors.html"><img src="../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../rtree_examples.html"><img src="../../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../../index.html"><img src="../../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="index_stored_in_shared_memory_using_boost_interprocess.html"><img src="../../../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
</body>
</html>
